﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using YJ.Platform;

namespace WebMvc.Common
{
    public class CustomFormSave
    {
        public static object GetJson(YJ.Data.Model.WorkFlowCustomEventParams eventParams)
        {
            return new YJ.Data.MSSQL.DBHelper().GetDataTable("select * from users");
        }

        public static string QianShi(YJ.Data.Model.WorkFlowCustomEventParams eventParams)
        {
            string t = System.Web.HttpContext.Current.Request["TempTest.Title"];
            YJ.Platform.Log.Add("获取值测试", t + "--");
            return "";
        }
        public static string UpdateSample(YJ.Data.Model.WorkFlowCustomEventParams eventParams)
        {
            try
            {

            
            YJ.Data.MSSQL.DBHelper db = new YJ.Data.MSSQL.DBHelper();
            DataTable samples=db.GetDataTable(string.Format("select *,b.name from AmApplySample a left join Organize b on a.UDeptID=Convert(nvarchar(50),b.id) where a.id='{0}'", eventParams.InstanceID));
          db.Execute(string.Format("INSERT INTO [AmSampleInOut]([AmsAampleId],[UseUId],[Address1])VALUES('{0}','{1}','{2}')", samples.Rows[0]["SampleId"], samples.Rows[0]["UID"], samples.Rows[0]["name"]));
                db.Execute(string.Format(" update AmSample set DisposeResult=2 where  '{0}' like '%'+bgbh+'%'", samples.Rows[0]["SampleId"]));
                return "1";
            }
            catch (Exception e)
            {
                YJ.Platform.Log.Add("样品调用审批流程-发放样品-提交后事件", e.Message, YJ.Platform.Log.Types.流程相关);
                return "0";
            }
        }
        /// <summary>
        /// 测试用方法返回人员
        /// </summary>
        /// <param name="eventParams"></param>
        /// <returns></returns>
        public static string GetMembers(YJ.Data.Model.WorkFlowCustomEventParams eventParams)
        {

            return  "u_EB03262C-AB60-4BC6-A4C0-96E66A4229FE,u_8086D01F-7AE3-402E-B543-D34F1059F79A";
        }
        /// <summary>
        /// 发送会议通知
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string SendNotice(YJ.Data.Model.WorkFlowCustomEventParams data)
        {
            YJ.Data.MSSQL.DBHelper db = new YJ.Data.MSSQL.DBHelper();
            YJ.Platform.Users user = new YJ.Platform.Users();
            YJ.Platform.WorkFlowTask btask = new YJ.Platform.WorkFlowTask();
            YJ.Data.Model.WorkFlowTask task = new YJ.Data.Model.WorkFlowTask();
            YJ.Platform.ShortMessage SM = new YJ.Platform.ShortMessage();
            YJ.Data.Model.ShortMessage sm = new YJ.Data.Model.ShortMessage();
            Users bUsers = new Users();
            ShortMessage shorMsg = new ShortMessage();
            string ID = data.InstanceID;
            string sql = "select Participants from CRMeetingRequest where ID='" + ID + "'";
            string Participants = db.ExecuteScalar(sql);
            string[] strArray = Participants.Split(','); //字符串转数组
            Guid groupID = Guid.NewGuid();
            foreach (var item in strArray)
            {
                string userid = user.RemovePrefix1(item);
                task.FlowID = data.FlowID;
                task.GroupID = data.GroupID;
                task.ID = Guid.NewGuid();
                task.Type = 0;
                task.InstanceID = data.InstanceID;
                task.Note = "会议通知";
                task.PrevID = "".ToGuid();
                task.PrevStepID = "950bc1a7-2be7-461a-a982-b6d1a87be1a2".ToGuid();
                task.ReceiveID = userid.ToGuid();
                task.ReceiveName = user.GetName(userid.ToGuid());
                task.ReceiveTime = YJ.Utility.DateTimeNew.Now;
                task.SenderID = YJ.Platform.Users.CurrentUserID;
                task.SenderName = YJ.Platform.Users.CurrentUserName;
                YJ.Platform.WeiXin.Agents bAgents = new YJ.Platform.WeiXin.Agents();
                task.SenderTime = task.ReceiveTime;
                task.Status = 0;
                task.StepID = "029b5b49-adbd-4647-be26-1fe2c9196d94".ToGuid();
                task.StepName = "参会员工回执";
                task.Sort = 0;
                task.Title = "会议通知";
                btask.Add(task);
                Guid msgID = Guid.NewGuid();
                string msgContents = "您有一个新的待办任务，流程:会议通知。";
                string linkUrl = "javascript:openApp('/WorkFlowRun/Index?flowid=" + task.FlowID + "&stepid=" + task.StepID + "&instanceid=" + task.InstanceID + "&taskid=" + task.ID + "&groupid=" + task.GroupID + "',0,'" + task.Title.RemoveHTML().RemovePunctuationOrEmpty() + "','tab_" + task.ID + "');closeMessage('" + msgID + "');";
                ShortMessage.Send(task.ReceiveID, task.ReceiveName, "流程待办提醒", msgContents, 1, linkUrl, task.ID.ToString(), msgID.ToString(), groupID.ToString());
                new YJ.Platform.WeiXin.Message().SendText(msgContents, bUsers.GetAccountByID(task.ReceiveID), agentid: bAgents.GetAgentIDByCode("weixinagents_waittasks"), async: true);
            }
            return "通知已发送";

        }

        /// <summary>
        /// 发送会议通知
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string UpdataCar1(YJ.Data.Model.WorkFlowCustomEventParams data)
        {
            YJ.Data.MSSQL.DBHelper db = new YJ.Data.MSSQL.DBHelper();
            YJ.Platform.Users user = new YJ.Platform.Users();
            YJ.Platform.WorkFlowTask btask = new YJ.Platform.WorkFlowTask();
            YJ.Data.Model.WorkFlowTask task = new YJ.Data.Model.WorkFlowTask();
            YJ.Platform.ShortMessage SM = new YJ.Platform.ShortMessage();
            YJ.Data.Model.ShortMessage sm = new YJ.Data.Model.ShortMessage();
            Users bUsers = new Users();
            ShortMessage shorMsg = new ShortMessage();
            string ID = data.InstanceID;
            string sql1= "select CarNumber from OaVehicleApplication where ID='"+ID+"'";
            string CARID= db.ExecuteScalar(sql1);
            string sql = "update OaCar set Status='1' where ID='"+ CARID + "'";
            db.Execute(sql);
            return "修改车辆状态成功";
        }
        /// <summary>
        /// 发送会议通知
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string UpdataCar0(YJ.Data.Model.WorkFlowCustomEventParams data)
        {
            YJ.Data.MSSQL.DBHelper db = new YJ.Data.MSSQL.DBHelper();
            YJ.Platform.Users user = new YJ.Platform.Users();
            YJ.Platform.WorkFlowTask btask = new YJ.Platform.WorkFlowTask();
            YJ.Data.Model.WorkFlowTask task = new YJ.Data.Model.WorkFlowTask();
            YJ.Platform.ShortMessage SM = new YJ.Platform.ShortMessage();
            YJ.Data.Model.ShortMessage sm = new YJ.Data.Model.ShortMessage();
            Users bUsers = new Users();
            ShortMessage shorMsg = new ShortMessage();
            string ID = data.InstanceID;
            string sql1 = "select CarNumber from OaVehicleApplication where ID='" + ID + "'";
            string CARID = db.ExecuteScalar(sql1);
            string sql = "update OaCar set Status='0' where ID='" + CARID + "'";
            db.Execute(sql);
            return "修改车辆状态成功";
        }
        /// <summary>
        /// 子流程激活前事件（示例）
        /// </summary>
        /// <param name="eventParams"></param>
        /// <returns></returns>
        public static YJ.Data.Model.WorkFlowExecute.Execute SubFlowActivationBefore(YJ.Data.Model.WorkFlowCustomEventParams eventParams)
        {
            YJ.Data.Model.WorkFlowExecute.Execute execute = new YJ.Data.Model.WorkFlowExecute.Execute();
            
            //在这里添加插入子流程业务数据代码

            YJ.Platform.Log.Add("执行了子流程激活前事件", "", YJ.Platform.Log.Types.其它分类);
            //execute.Title = "";
            return execute;
        }

        /// <summary>
        /// 子流程结束后事件（示例）
        /// </summary>
        /// <param name="eventParams"></param>
        /// <returns></returns>
        public static void SubFlowCompletedBefore(YJ.Data.Model.WorkFlowCustomEventParams eventParams)
        {
           
            //在这里添加子流程结束后代码

            YJ.Platform.Log.Add("执行了子流程结束后事件", "", YJ.Platform.Log.Types.其它分类);
        }
    }

    
}